package qdu.com.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.hdfs.FileUtil;
import qdu.com.bean.WJsonBean4;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/rentp")
public class RentPerMonthByWeatherServlet extends HttpServlet {
    ObjectMapper mapper = new ObjectMapper();
    List<WJsonBean4> list = new ArrayList<WJsonBean4>();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<Integer, String[]> items = FileUtil.ReadPartFile(getServletContext().getRealPath("/output/output")+"/part-r-00004");
        for (Integer key:
                items.keySet()) {
            String[] value = items.get(key); // 3列数据
            list.add(new WJsonBean4(value[0],value[1],value[2]));
        }
        String json = mapper.writeValueAsString(list);
        resp.setHeader("content-type", "text/json;charset=utf-8");
        resp.getWriter().write(json);
    }

//     test
//    public static void main(String[] args) throws IOException {
//        ObjectMapper mapper = new ObjectMapper();
//        List<WJsonBean4> list = new ArrayList<WJsonBean4>();
//        Map<Integer, String[]> items = FileUtil.ReadPartFile("output/part-r-00004");
//        for (Integer key:
//                items.keySet()) {
//            String[] value = items.get(key); // 3列数据
//            list.add(new WJsonBean4(value[0],value[1],value[2]));
//        }
//        for (WJsonBean4 j:
//             list) {
//            String e1=j.getTime();
//            String e2=j.getWeather();
//            String e3=j.getCount();
//            System.out.println(e1+" "+e2+" "+e3);
//        }
//    }

}
